package com.taoyibao.hongcanting.ui.home.views;

import android.content.Context;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.view.View;

import com.taoyibao.hongcanting.R;
import com.taoyibao.hongcanting.base.BaseRelativeLayout;
import com.taoyibao.hongcanting.cache.SpUtils;
import com.taoyibao.hongcanting.model.DishModel;
import com.taoyibao.hongcanting.ui.home.activity.MenuActivity;
import com.taoyibao.hongcanting.ui.login.activity.LoginActivity;
import com.taoyibao.hongcanting.utils.SpaceItemDecoration;
import com.zhy.adapter.recyclerview.CommonAdapter;
import com.zhy.adapter.recyclerview.MultiItemTypeAdapter;
import com.zhy.adapter.recyclerview.base.ViewHolder;

import java.util.ArrayList;
import java.util.List;

/**
 * @author TangAnna
 * @description: 首页的特色菜品
 * @date :${DATA} 14:16
 */
public class FeaturesArea extends BaseRelativeLayout<List<DishModel>> implements View.OnClickListener {
    private RecyclerView mRecyclerView;
    private CommonAdapter mAdapter;
    private List<DishModel> mData;

    public FeaturesArea(Context context) {
        super(context);
    }

    public FeaturesArea(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public FeaturesArea(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    protected void initView(Context context, AttributeSet attrs, int defStyleAttr) {
        mData = new ArrayList<>();
        findViewById(R.id.tv_area_features_more).setOnClickListener(this);
        mRecyclerView = findViewById(R.id.rv_features);
        mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2, LinearLayoutManager.VERTICAL, false));
        mRecyclerView.addItemDecoration(new SpaceItemDecoration(18, 24));
        mRecyclerView.setAdapter(mAdapter = new CommonAdapter<DishModel>(getContext(), R.layout.item_features_shell, mData) {
            @Override
            protected void convert(ViewHolder viewHolder, DishModel dishModel, int position) {
                ItemFeaturesArea itemFeaturesArea = (ItemFeaturesArea) viewHolder.getConvertView();
                itemFeaturesArea.setData(dishModel);
            }
        });
        //Item点击事件
        mAdapter.setOnItemClickListener(new MultiItemTypeAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, RecyclerView.ViewHolder holder, int position) {
                if (SpUtils.getLoginStatus()) {
                    MenuActivity.open(getContext(), mData.get(position));
                } else {
                    LoginActivity.open(getContext(), "");
                }

            }

            @Override
            public boolean onItemLongClick(View view, RecyclerView.ViewHolder holder, int position) {
                return false;
            }
        });
    }

    @Override
    public void setData(final List<DishModel> data) {
        if (data == null || data.size() < 1) {
            return;
        }
        mData.clear();
        mData.addAll(data);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    protected int bindLayout() {
        return R.layout.area_features;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tv_area_features_more://更多操作
                if (SpUtils.getLoginStatus()) {
                    MenuActivity.open(getContext());
                } else {
                    LoginActivity.open(getContext(), "");
                }
                break;
        }
    }
}
